package sh.wn36.vehiclemanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import sh.wn36.vehiclemanage.entity.CurrentUnit;
import sh.wn36.vehiclemanage.entity.Dictionary;
import sh.wn36.vehiclemanage.service.CurrentUnitService;
import sh.wn36.vehiclemanage.service.DictionaryService;
import sh.wn36.vehiclemanage.util.Result;
import sh.wn36.vehiclemanage.xzy.Query;
import sh.wn36.vehiclemanage.xzy.UnitQuery;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xk
 * @since 2021-04-26
 */
@Api("往来单位")
@CrossOrigin
@RestController
@RequestMapping("/current-unit")
public class CurrentUnitController {

    @Autowired
    private CurrentUnitService unitService;

    @Autowired
    private DictionaryService dictionaryService;

    @ApiOperation("根据条件分页查询")
    @PostMapping("/pageUnitByConditon/{current}/{limit}")
    public Result pageDriverByConditon(@ApiParam(value = "当前页码", required = true, name = "currentPage") @PathVariable("current") Integer current,
                                       @ApiParam(value = "当前页数", required = true, name = "pageSize") @PathVariable("limit") Integer limit,
                                       @ApiParam(value = "查询条件", required = false, name = "unitQuery") @RequestBody(required = false) Query query) {
        Page<UnitQuery> currentUnitPage = new Page<>(current, limit);
        QueryWrapper<UnitQuery> queryWrapper = new QueryWrapper<>();
        String name = query.getUnit_name();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("unit_name", name);
        }
        unitService.getAll(currentUnitPage, queryWrapper);
        long total = currentUnitPage.getTotal();
        List<UnitQuery> records = currentUnitPage.getRecords();
        return Result.OK().data("total", total).data("records", records);
    }

    @ApiOperation("添加单位")
    @PostMapping("/saveUnit")
    public Result saveDriver(@RequestBody UnitQuery currentUnit) {
        boolean save = unitService.save(currentUnit);
        if (save)
            return Result.OK();
        else
            return Result.Fail();
    }

    @ApiOperation("删除单位")
    @GetMapping("/delUnit/{ids}")
    public Result delDriver(@ApiParam(value = "单位编号", required = true, name = "id")
                            @PathVariable List<Integer> ids) {
        boolean b = unitService.removeByIds(ids);
        if (b) {
            return Result.OK();
        } else {
            return Result.Fail();
        }

    }

    @ApiOperation("修改单位")
    @PostMapping("/updateUnit")
    public Result updateDriver(@RequestBody UnitQuery currentUnit) {
        boolean update = unitService.updateById(currentUnit);
        if (update) {
            return Result.OK();
        } else {
            return Result.Fail();
        }
    }

    @ApiOperation("根据id查找单位")
    @GetMapping("/findUnitById/{id}")
    public Result findUnitById(@ApiParam(value = "单位编号", required = true, name = "id") @PathVariable("id") Integer id) {
        CurrentUnit unit = unitService.getById(id);
        return Result.OK().data("unit", unit);
    }

    @ApiOperation("查找单位信息")
    @GetMapping("/selOne")
    public Result selOne() {
        List<Dictionary> dictionaryList = dictionaryService.listById();
        return Result.OK().data("dictionaryList", dictionaryList);
    }
}